'''
Created on Oct 11, 2012

@author: daeheehan
'''
from pprint import pprint
from github3 import login, GitHub
from getpass import getpass, getuser
from github import Github

import datetime
import dateutil.parser
import time
from pprint import pprint
import sys

# Github User information
GITHUB_USER_ID = "booknara"
GITHUB_USER_ID_1 = "booknaratest"
GITHUB_USER_ID_2 = "jangho87"
GITHUB_USER_ID_3 = "luispigu"

GITHUB_USER_PW = "qwer1234"
GITHUB_USER_PW_1 = 'wasd1234'

if __name__ == '__main__':
    pass

gh = login(GITHUB_USER_ID_3, password=GITHUB_USER_PW_1)
print gh.ratelimit_remaining, 'request remaining'

# Github Repository information
GITHUB_USER_NAME = 'symfony'
GITHUB_REPOSITORY_NAME = 'symfony'

# Collaborator followers information file
COLLABORATORS_FOLLOWER_FILE = GITHUB_REPOSITORY_NAME + '_collaborators_follower.txt'

project_collaborators = []
collaborators_follower = {}

try:
    g = Github(GITHUB_USER_ID_1, GITHUB_USER_PW)
    print str(g.rate_limiting) + ' request remaining'
    
    user = g.get_user(GITHUB_USER_NAME)
    repo = user.get_repo(GITHUB_REPOSITORY_NAME)
    
    for name in repo.get_collaborators():
        project_collaborators.append(name.login)

    print 'loading contributors file is finished'

    for item in project_collaborators:
        print 'login id: ' +  item
        user = gh.user(item)

#        print user.followers
        collaborators_follower[item] = user.followers
                
        if(gh.ratelimit_remaining < 100):
            time.sleep(60 * 60)
#        break
    
    f_collaborators_follower = open(COLLABORATORS_FOLLOWER_FILE, 'w')
    print 'Printing dictionary: collaborator followers'
    for key, value in collaborators_follower.items():
        sys.stdout.write(key + ", " + str(value) + '\n')
        f_collaborators_follower.write(key + ", " + str(value) + '\n')

    f_collaborators_follower.close()


except Exception as e: 
    print 'The final Exception' + str(e.args[0])

print gh.ratelimit_remaining, ' request remaining'

print
print 'Finally the end'